package com.kevin.d;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.kev.library.adapter.CommonRecyclerViewAdapter;
import com.kev.library.adapter.CommonRecyclerViewHolder;
import com.kev.library.widget.DividerGridItemDecoration;
import com.kevin.d.bean.AreaRes;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 非通用
 */
@Deprecated
public class LinkListActivity extends AppCompatActivity implements
        OnLoadMoreListener, OnRefreshListener, View.OnClickListener {

    private RecyclerView rv;
    private List<AreaRes> data = new ArrayList<>();//总数据
    private List<AreaRes> dataTemp = new ArrayList<>();//需要展示的数据
    private List<List<AreaRes>> areaHistoryData = new ArrayList<>();//记录每一层级的数据,返回时使用
    private List<String[]> areaName = new ArrayList<>();//记录点击的某一项的名字
    private CommonRecyclerViewAdapter<AreaRes> adapter;

    private TextView tvBackLevel;
    private TextView tvCurLevel;
    private TextView tvFlag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_link_list);
        initView();
        initData();
    }


    private void initView() {
        tvBackLevel = findViewById(R.id.tv_back_level);
        tvCurLevel = findViewById(R.id.tv_cur_level);
        tvFlag = findViewById(R.id.tv_flag);
        rv = findViewById(R.id.rv);

        tvFlag.setVisibility(View.GONE);
        tvBackLevel.setOnClickListener(this);

        rv.setLayoutManager(new LinearLayoutManager(this));
        //分割线
        rv.addItemDecoration(new DividerGridItemDecoration(this, DividerGridItemDecoration.VERTICAL_LIST));
        rv.setItemAnimator(new DefaultItemAnimator());
        RefreshLayout refreshLayout = findViewById(R.id.refresh_layout);
        refreshLayout.setOnLoadMoreListener(this);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));
        refreshLayout.setRefreshHeader(new ClassicsHeader(this).setEnableLastTime(true));
    }

    private void initData() {
        Gson gson = new Gson();
        data = gson.fromJson(AreaRes.jsonArray, new TypeToken<List<AreaRes>>() {
        }.getType());
        dataTemp.addAll(data);

        areaHistoryData.add(data);
        areaName.add(new String[]{"全部", ""});

        adapter = new CommonRecyclerViewAdapter<AreaRes>(this, dataTemp) {
            @Override
            public void convert(CommonRecyclerViewHolder h, AreaRes entity, int position) {
                TextView tvAddress = h.getView(R.id.tv_name);
                tvAddress.setText(entity.getCeaName());
                ImageView ivArrow = h.getView(R.id.iv_arrow);
                if (entity.getAreaList() == null || entity.getAreaList().size() == 0) {
                    ivArrow.setVisibility(View.GONE);
                } else {
                    ivArrow.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_area;
            }
        };
        adapter.setOnRecyclerViewItemClickListener(new CommonRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                List<AreaRes> mDatas = new ArrayList<>(dataTemp);
                if (mDatas.get(position).getAreaList() != null
                        && mDatas.get(position).getAreaList().size() > 0) {
                    //更新数据，刷新列表
                    dataTemp.clear();
                    dataTemp.addAll(mDatas.get(position).getAreaList());
                    areaHistoryData.add(mDatas.get(position).getAreaList());
                    adapter.notifyDataSetChanged();

                    //显示层级名称，并记录
                    String backName;
                    String curName = mDatas.get(position).getCeaName();
                    if (areaHistoryData.size() <= 1) {
                        backName = "全部";
                    } else {
                        tvFlag.setVisibility(View.VISIBLE);
                        backName = TextUtils.isEmpty(areaName.get(areaName.size() - 1)[1]) ? "全部"
                                : areaName.get(areaName.size() - 1)[1];
                    }
                    tvBackLevel.setText(backName);
                    tvCurLevel.setText(curName);
                    areaName.add(new String[]{backName, curName});
                }
            }
        });
        rv.setAdapter(adapter);
    }

    @Override
    public void onBackPressed() {
        if (!allowBack()) {
            super.onBackPressed();
        }
    }

    /**
     * 是否可以返回
     */
    private boolean allowBack() {
        if (areaHistoryData.size() > 1) {
            dataTemp.clear();
            dataTemp.addAll(areaHistoryData.get(areaHistoryData.size() - 2));//展示记录的倒数第二个数据集
            areaHistoryData.remove(areaHistoryData.size() - 1);//移除掉倒数第一个数据集
            adapter.notifyDataSetChanged();

            tvCurLevel.setText(areaName.get(areaName.size() - 2)[1]);
            tvBackLevel.setText(areaName.get(areaName.size() - 2)[0]);
            areaName.remove(areaName.size() - 1);
            if (areaName.size() == 1) {
                tvFlag.setVisibility(View.GONE);
            }
            return true;
        }
        return false;
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        //刷新列表
        refreshLayout.finishRefresh(2000);
    }

    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {
        //加载更多
        refreshLayout.finishLoadMore(2000);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back_level:
                allowBack();
                break;
            default:
                break;
        }
    }
}
